<template>
	<view class="forget-pwd-container">
		<view class="header-decoration"></view>

		<view class="form-box">
			<view class="title">重置密码</view>

			<view class="input-group">
				<view class="input-box">
					<input v-model="form.username" placeholder="请输入用户名" />
				</view>

				<view class="input-box">
					<input v-model="form.password" placeholder="请输入新密码" :password="showPassword" />
					<view @click="showPassword = !showPassword">
						<uni-icons :type="showPassword ? 'eye' : 'eye-slash'" size="30"></uni-icons>
					</view>
				</view>

				<view class="input-box">
					<input v-model="form.confirmPassword" placeholder="请确认新密码" :password="showConfirmPassword" />
					<view @click="showConfirmPassword = !showConfirmPassword">
						<uni-icons :type="showConfirmPassword ? 'eye' : 'eye-slash'" size="30"></uni-icons>
					</view>
				</view>
			</view>

			<view class="btn-group">
				<button class="submit-btn" @click="handleSubmit">确认重置</button>
				<view class="back-btn" @click="goBack">返回登录</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onMounted,
		onBeforeUnmount
	} from 'vue'
	import { getThemeStyles } from '@/stores/theme'

	// 定义响应式数据
	const showPassword = ref(false)
	const showConfirmPassword = ref(false)
	const form = ref({
		username: '',
		password: '',
		confirmPassword: ''
	})

	// 验证用户名格式
	const validateUsername = (username) => {
		return username && username.length >= 3
	}

	// 处理提交
	const handleSubmit = () => {
		if (!form.value.username || !form.value.password || !form.value.confirmPassword) {
			uni.showToast({
				title: '请填写完整信息',
				icon: 'none'
			})
			return
		}

		if (!validateUsername(form.value.username)) {
			uni.showToast({
				title: '用户名至少3个字符',
				icon: 'none'
			})
			return
		}

		if (form.value.password.length < 6) {
			uni.showToast({
				title: '密码至少6个字符',
				icon: 'none'
			})
			return
		}

		if (form.value.password !== form.value.confirmPassword) {
			uni.showToast({
				title: '两次密码不一致',
				icon: 'none'
			})
			return
		}

		// 这里添加重置密码的实际逻辑
		uni.showToast({
			title: '密码重置成功',
			icon: 'success',
			duration: 2000,
			success: () => {
				setTimeout(() => {
					goBack()
				}, 2000)
			}
		})
	}

	// 返回登录
	const goBack = () => {
		uni.navigateBack()
	}

	// 获取主题样式
	const themeStyles = computed(() => getThemeStyles());

	// 监听主题变化
	onMounted(() => {
		uni.$on('themeChange', () => {
			// 主题切换时，computed 会自动更新
		});
	});

	// 清理事件监听
	onBeforeUnmount(() => {
		uni.$off('themeChange');
	});
</script>
<style lang="scss">
	/* stylelint-disable */
	.forget-pwd-container {
		min-height: 100vh;
		background: #f5f6fa;
		position: relative;
		padding: 0 40rpx;
		display: flex;
		flex-direction: column;
	}

	.header-decoration {
		height: 42vh;
		background: v-bind('themeStyles.primaryGradient');
		margin: 0 -40rpx;
		position: relative;

		&::after {
			content: '';
			position: absolute;
			bottom: -60rpx;
			left: 0;
			right: 0;
			height: 120rpx;
			background: #f5f6fa;
			border-radius: 50% 50% 0 0;
			transform: scaleX(1.5);
		}
	}

	.form-box {
		margin-top: -17vh;
		background: rgba(255, 255, 255, 0.95);
		border-radius: 30rpx;
		padding: 60rpx 60rpx;
		box-shadow: v-bind('themeStyles.cardBoxShadow');
		backdrop-filter: blur(10px);
		border: 1px solid rgba(255, 255, 255, 0.9);

		.title {
			font-size: 40rpx;
			font-weight: 600;
			text-align: center;
			margin-bottom: 80rpx;
			background: v-bind('themeStyles.primaryGradient');
			background-clip: text;
			-webkit-background-clip: text;
			color: transparent;
		}
	}

	.input-group {
		.input-box {
			display: flex;
			align-items: center;
			background: rgba(255, 255, 255, 0.95);
			border-radius: 16rpx;
			margin-bottom: 40rpx;
			padding: 24rpx 40rpx;
			box-shadow: v-bind('themeStyles.shadowSm');
			border: 1px solid v-bind('themeStyles.borderLight');

			input {
				flex: 1;
				font-size: 28rpx;
				color: v-bind('themeStyles.textPrimary');

				&::placeholder {
					color: v-bind('themeStyles.textPlaceholder');
				}
			}
		}
	}

	.btn-group {
		margin-top: 50rpx;

		.submit-btn {
			background: v-bind('themeStyles.primaryGradient');
			color: #fff;
			border: none;
			border-radius: 50rpx;
			font-size: 30rpx;
			font-weight: 500;
			letter-spacing: 8rpx;
			box-shadow: v-bind('themeStyles.buttonBoxShadow');
			width: 85%;
			margin: 0 auto;
			transition: all 0.3s ease;

			&:active {
				transform: translateY(2rpx);
				box-shadow: v-bind('themeStyles.cardBoxShadow');
				opacity: 0.95;
			}
		}

		.back-btn {
			text-align: center;
			margin-top: 40rpx;
			padding: 20rpx;
			font-size: 26rpx;
			color: v-bind('themeStyles.primaryColor');

			&:active {
				opacity: 0.8;
			}
		}
	}
</style>